define(['zepto'], function ($) {
   var b = {
        init: function (g) {
            var d = {
                yesText: "ok",
                type:"0",
                noText: "cancel",
                close: true,
                title: "Message",
                content: "",
                closeBtn: "",
                layerClose:true,
            };
            var c = $.extend(d, g);
            var i = this;
            var h = $(".layerBox").size();
            var f = '<div class="layerBox mylayer '+(c.no_remove?'no_remove':'')+' '+c.class_name+'" index="'+h+'"  id="layerBox' + h +'">';
            f+='<div class="layerBox_wrapper"><div class="layerBox_content">';
            if(c.title){
                f+='<div class="layerBox_title">';
                f += c.title + "</div>";
            }
            f += '<div class="layerBox_text">' + c.content + '</div><div class="layerBox_footer" align="center">';
            f += "</div></div></div>";
            $("body").append(f);
            $("body").css("overflow-y","hidden");
            if (c.success && typeof c.success == "function") {
                c.success()
            }
            var z_index=10000 + h + 1;
            if(z_index<$(".layer_login").css('z-index')){
                z_index=$(".layer_login").css('z-index')+2;
            }
            $("#layerBox" + h).css({"display": "block","z-index": z_index});
            var j = $("#layerBox" + h).find(".layerBox_wrapper");
            j.addClass("pageRight");
                j.css({
                    left: "0px",
                    top: "0px",
                    width:"100%",
                    "margin-left": "0px",
                    "margin-top": "0px"
            });
            $(".warp").hide();
            i.initEvent(h, c);
            $("#overlay" + h).css("z-index", z_index);
            return h
        },
        initEvent: function (d, c) {
            var e = this;
            $(".layerBox .close").off("click").on("click", function () {
                if (c && c.closeBtn) {
                    if (typeof c.closeBtn == "function") {
                        c.closeBtn()
                    }
                }
                var index=$(this).closest(".layerBox").attr("index");
                if(c.closeEvent&&typeof c.closeEvent=="function"){
                    c.closeEvent();
                }
                e.closeLayer(index);
            });
        },
        closeLayer: function (c) {
            if($("#layerBox" + c).hasClass('no_remove')){
                $("#layerBox" + c).hide();
                $("#overlay" + c).hide();
            }
            else{
                if ($("#layerBox" + c).size() > 0) {
                    $("#layerBox" + c).remove()
                }
                $("#overlay" + c).remove();
            }
            if (($(".mylayer").size() == 0||$(".mylayer.no_remove:visible").length==0)) {
                $("body").css("overflow-y", "auto");
            }
            $("body").css("overflow-y","auto");
            $(".warp").show();
        },
        showLayer:function(op){
            var site_url=window.location.href;
            var _this=this;
            var html='<div  class="header clearfix">'+
                      '<a class="icon pull-left close icon-back"></a>'+
                      '<h1 class="title page_title">'+op.title+'</h1>'+
                    '</div>';
            html=html+'<div class="layerContent">'+op.content+'</div>';
            var page=_this.init({
                title:'',
                content:html,
                overlay: false,
                class_name:op.class_name,
                no_remove:op.no_remove,
                success:function(){
                    if(typeof op.success=="function"){
                        op.success();
                    }
                },
                closeEvent:function(){
                    $("body").css("overflow-y", "auto");
                    $(".warp").show();
                    if (history.pushState){
                        history.pushState({ title: document.title,url:site_url}, document.title,site_url);
                    }
                    if(typeof op.closeEvent=="function"){
                        op.closeEvent();
                    }
                }
            });
            if (history.pushState){
                history.pushState({url:op.url},'', op.url);
                $(window).on('popstate', function(e){
                    _this.closeLayer(page);
                    history.pushState({ title: document.title,url:site_url}, document.title,site_url);
                    $("body").css("overflow-y", "auto");
                    $(".warp").show();
                });
            }
        }
    };
    return b
});
